package com.kakavr.game.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.game.service.IGameBasicService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
/**
 * 游戏管理
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("gameBasic")
public class GameBasicController extends BaseController {
	@Resource
	private IGameBasicService gameBasicService;

	/**
	 * 添加游戏
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request) {
		Map<String, Object> map = this.getGameParam(request);
		gameBasicService.addGameBasic(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改游戏
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "游戏id");
		}
		Map<String, Object> map = this.getGameParam(request);
		map.put("id", id);
		gameBasicService.updateGameBasic(map);
		return MsgResult.okResult();
	}

	/**
	 * 获取前端页面提交的参数
	 * @param request
	 * @return
	 */
	private Map<String, Object> getGameParam(HttpServletRequest request) {
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "名称");
		}
		String category = request.getParameter("category");
		if (StringUtils.isBlank(category)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "分类");
		}
		String categoryId = request.getParameter("categoryId");
		if (StringUtils.isBlank(categoryId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "分类");
		}
		String path = request.getParameter("path");
		if (StringUtils.isBlank(path)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "本地路径");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", name);
		map.put("category", category);
		map.put("categoryId", categoryId);
		map.put("path", path);
		map.put("params", request.getParameter("params"));
		map.put("psName", request.getParameter("psName"));
		map.put("details", request.getParameter("describe"));
		map.put("imgs", request.getParameter("imgs"));
		return map;
	}

	/**
	 * 删除游戏
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public Map<String, Object> delete(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "游戏id");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("status", 0);
		gameBasicService.updateGameBasic(map);
		return MsgResult.okResult();
	}

	/**
	 * 游戏列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectList", method = RequestMethod.GET)
	public Map<String, Object> selectList(HttpServletRequest request) {
		Map<String, Object> map = this.initPaginationParams(request);
		return MsgResult.okResultData(gameBasicService
				.selectGameBasicByPage(map));
	}
	
	
	/**
	 * 游戏是否在首页显示
	 */
	@RequestMapping(value={"showhome"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    public Map<String, Object> gameshowhome(HttpServletRequest request)
    {
	    Map<String, Object> map = initPaginationParams(request);
	    String isHome = request.getParameter("isHome");
	    String id = request.getParameter("id");
	    map.put("isHome", isHome);
	    map.put("id", id);
	    this.gameBasicService.updateGameBasic(map);
	    return MsgResult.okResult();
    }

}
